﻿using System;
using System.Collections;
using PathFind.Algo;

namespace PathFind {
    public static class AStarMetrics {
        public static readonly Coordinate[] Offsets = {
            new Coordinate(0,-1),
            new Coordinate(1,-1),
            new Coordinate(1,0),
            new Coordinate(1,1),
            new Coordinate(0,1),
            new Coordinate(-1,1),
            new Coordinate(-1,0),
            new Coordinate(-1,-1),
            new Coordinate(0,0),
        };
        // public static short[] offsetsX = {0, 1, 1, 1, 0, -1, -1, -1, 0};
        // public static short[] offsetsY = {-1, -1, 0, 1, 1, 1, 0, -1, 0};

        public static bool IsDiag(this Direction dir) {
            if (dir == Direction.CENTER) {
                return false;
            }
            return ((byte) dir & 1) == 1;
        }

        public static Direction AddRoundStep(this Direction dir, short roundStep) {
            return (Direction) (((byte) dir + roundStep) & 7);
        }

        public static Coordinate DirOffset(this Direction dir) {
            return Offsets[(short) dir];
        }

        public static Coordinate AddDirOffset(this Coordinate co, Direction dir) {
            return co + Offsets[(short) dir];
        }

        public static Coordinate AddDirPreviousOffset(this Coordinate co, Direction dir) {
            return co + Offsets[((byte) dir + 7) & 7];
        }
        public static Coordinate AddDirNextOffset(this Coordinate co, Direction dir) {
            return co + Offsets[((byte) dir + 1) & 7];
        }
        public static Coordinate AddDirPlusRoundStep(this Coordinate co, Direction dir, short i) {
            return co + Offsets[((byte) dir + i) & 7];
        }
        public static int BitArrayLength(short width, short height) {
            return (width * height + 7) / 8;
        }
        public static byte[] ToByteArray(this BitArray bits, short width, short height)
        {
            byte[] ret = new byte[BitArrayLength(width, height)];
            bits.CopyTo(ret, 0);
            return ret;
        }
    }
}